package demo3;

import java.util.concurrent.Semaphore;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-08-11
 * Time: 17:24
 */
public class TestDemo1 {
    /**
     * 编写代码实现两个线程增加同一个变量
     * 使用 Semphore 来控制线程安全.
     */
    public static void main(String[] args) {
        //设置信号量可用资数为4
        Semaphore semaphore = new Semaphore(4);
        Thread t1 = new Thread(()->{
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        Thread t2 = new Thread(()->{

        });
        t2.start();
    }

}
